package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {


    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车功能
     *
     * @param shoppingCartDTO
     * @return
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //     处理逻辑
        //     1判断选择的商品是否已经存在
        ShoppingCart shoppingCart = new ShoppingCart();
        //     数据拷贝
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //     获取用户id
        Long userId = BaseContext.getCurrentId();
        //     设置userId
        shoppingCart.setUserId(userId);

        //      操作mapper层
        List<ShoppingCart> list = shoppingCartMapper.lists(shoppingCart);
        //     2如果已经存在则数据加一
        if (list != null && list.size() > 0) {
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber() + 1);
            //     修改数据 使其数量加一
            shoppingCartMapper.updateNumberById(cart);
        } else {
            //     3不存在则操作数据库增加一条数据
            // 判断本次添加进购物车的是菜品还是套餐
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null) {
                Dish dish = dishMapper.getById(dishId);
                //     设置购物车数据
                shoppingCart.setName(dish.getName());
                //     图片
                shoppingCart.setImage(dish.getImage());
                //     数量
                shoppingCart.setAmount(dish.getPrice());
            } else {
                // 本次添加为套餐
                Long setmealId = shoppingCartDTO.getSetmealId();
                // 套餐对象
                Setmeal setmeal = setmealMapper.getById(setmealId);
            //     设置数据值
            //     名称
                shoppingCart.setName(setmeal.getName());
                // 图片
                shoppingCart.setImage(setmeal.getImage());
                // 价格
                shoppingCart.setAmount(setmeal.getPrice());
            }
            //     设置数量
            shoppingCart.setNumber(1);
            //     设置时间
            shoppingCart.setCreateTime(LocalDateTime.now());
            System.out.println("商品信息" + shoppingCart);
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        // 获取微信用户ID
        Long userId = BaseContext.getCurrentId();
        // 构建实例对象
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(userId)
                .build();
        List<ShoppingCart> lists = shoppingCartMapper.lists(shoppingCart);
        return lists;
    }

    @Override
    public void clean() {
        // 获取微信用户ID
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.clean(userId);
    }

    @Override
    public void deleteById(ShoppingCartDTO shoppingCartDTO) {
            shoppingCartMapper.deleteByid(shoppingCartDTO);
    }
}
